﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace Common
{
   public class SYHIdentityTicket
    {
       /// <summary>
       /// 获取票据身份信息
       /// </summary>
       /// <returns></returns>
       public static string GetIdentity()
       {
           HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
           string identitydata = string.Empty;
           if (cookie != null)
           {
               FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
               if (ticket != null)
               {
                   identitydata = ticket.UserData;
               }
           }
           return identitydata;

       }

       /// <summary>
       /// 设置票据信息 
       /// </summary>
       /// <param name="userdata"></param>
       /// <param name="userid"></param>
       /// <param name="isRemember"></param>
       public static void SetAuthCookie(string userdata, int userid, bool isRemember)
       {

           FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userid.ToString(), DateTime.Now, DateTime.Now.AddHours(1), isRemember, userdata);
           //加密票据
           string authTicket = FormsAuthentication.Encrypt(ticket);
           //存储为cookie
           HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, authTicket);
           cookie.Domain = ".qihuiwang.com";
           cookie.Expires = ticket.Expiration;
           cookie.Path = "/";
           HttpContext.Current.Response.Cookies.Add(cookie);
           HttpContext.Current.Response.Write(HttpContext.Current.User.Identity.IsAuthenticated);
           HttpContext.Current.Response.End();
       }

    }
}
